using System.ComponentModel.DataAnnotations;

namespace Warehouse.Dto
{
    /// <summary>
    /// 创建调拨单请求DTO
    /// </summary>
    public class CreateAllocationRequest
    {
        [Required]
        public string TransferType { get; set; } = string.Empty;

        [Required]
        public DateTime ApplyDate { get; set; }

        [Required]
        public int FromWarehouseId { get; set; }

        [Required]
        public int ToWarehouseId { get; set; }

        public string? HandlerName { get; set; }
        public string? ApplicantName { get; set; }
        public string? Remark { get; set; }
        public bool SkipValidation { get; set; } = true;

        [Required]
        public List<CreateAllocationLineDto> Lines { get; set; } = new();
    }

    /// <summary>
    /// 创建调拨单明细DTO
    /// </summary>
    public class CreateAllocationLineDto
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; } = string.Empty;
        public string ProductNo { get; set; } = string.Empty;
        public string Specification { get; set; } = string.Empty;
        public string Unit { get; set; } = string.Empty;
        public int FromLocationId { get; set; }
        public int ToLocationId { get; set; }
        public decimal ReqQty { get; set; }
        public decimal UnitPrice { get; set; }
        public decimal FromWarehouseStock { get; set; }
        public decimal ToWarehouseStock { get; set; }
    }

    /// <summary>
    /// 创建调拨单响应DTO
    /// </summary>
    public class CreateAllocationResponse
    {
        public int TransferId { get; set; }
        public string TransferNo { get; set; } = string.Empty;
        public string Message { get; set; } = string.Empty;
    }

    /// <summary>
    /// 调拨单查询DTO
    /// </summary>
    public class AllocationQueryDto
    {
        public int Id { get; set; }
        public string TransferNo { get; set; } = string.Empty;
        public byte BillStatus { get; set; }
        public DateTime ApplyDate { get; set; }
        public string TransferType { get; set; } = string.Empty;
        public int FromWarehouseId { get; set; }
        public int ToWarehouseId { get; set; }
        public string FromWarehouseName { get; set; } = string.Empty;
        public string ToWarehouseName { get; set; } = string.Empty;
        public decimal TotalQty { get; set; }
        public decimal TotalAmount { get; set; }
        public string HandlerName { get; set; } = string.Empty;
        public string ApplicantName { get; set; } = string.Empty;
        public DateTime CreatedAt { get; set; }
    }

    /// <summary>
    /// 调拨单明细查询DTO
    /// </summary>
    public class AllocationLineQueryDto
    {
        public int Id { get; set; }
        public int TransferId { get; set; }
        public int LineNo { get; set; }
        public int ProductId { get; set; }
        public string ProductName { get; set; } = string.Empty;
        public string ProductNo { get; set; } = string.Empty;
        public string Specification { get; set; } = string.Empty;
        public string Unit { get; set; } = string.Empty;
        public int? FromLocationId { get; set; }
        public int? ToLocationId { get; set; }
        public string? FromLocationName { get; set; }
        public string? ToLocationName { get; set; }
        public decimal ReqQty { get; set; }
        public decimal OutQty { get; set; }
        public decimal InQty { get; set; }
        public decimal UnitPrice { get; set; }
    }

    /// <summary>
    /// 调拨出库请求DTO
    /// </summary>
    public class TransferOutRequest
    {
        [Required]
        public int TransferId { get; set; }

        [Required]
        public List<TransferOutLineDto> Lines { get; set; } = new();

        [StringLength(64)]
        public string OperatorName { get; set; } = string.Empty;

        [StringLength(256)]
        public string Remark { get; set; } = string.Empty;
    }

    /// <summary>
    /// 调拨出库明细DTO
    /// </summary>
    public class TransferOutLineDto
    {
        [Required]
        public int LineId { get; set; }

        [Required]
        public int ProductId { get; set; }

        [Required]
        public int WarehouseId { get; set; }

        public int? LocationId { get; set; }

        [Required]
        [Range(0.01, double.MaxValue, ErrorMessage = "出库数量必须大于0")]
        public decimal Qty { get; set; }

        [Required]
        [Range(0, double.MaxValue, ErrorMessage = "单价不能为负数")]
        public decimal UnitPrice { get; set; }
    }

    /// <summary>
    /// 调拨入库请求DTO
    /// </summary>
    public class TransferInRequest
    {
        [Required]
        public int TransferId { get; set; }

        [Required]
        public List<TransferInLineDto> Lines { get; set; } = new();

        [StringLength(64)]
        public string OperatorName { get; set; } = string.Empty;

        [StringLength(256)]
        public string Remark { get; set; } = string.Empty;
    }

    /// <summary>
    /// 调拨入库明细DTO
    /// </summary>
    public class TransferInLineDto
    {
        [Required]
        public int LineId { get; set; }

        [Required]
        public int ProductId { get; set; }

        [Required]
        public int WarehouseId { get; set; }

        public int? LocationId { get; set; }

        [Required]
        [Range(0.01, double.MaxValue, ErrorMessage = "入库数量必须大于0")]
        public decimal Qty { get; set; }

        [Required]
        [Range(0, double.MaxValue, ErrorMessage = "单价不能为负数")]
        public decimal UnitPrice { get; set; }
    }

    /// <summary>
    /// 调拨出库响应DTO
    /// </summary>
    public class TransferOutResponse
    {
        public int TransferId { get; set; }
        public string TransferNo { get; set; } = string.Empty;
        public string Message { get; set; } = string.Empty;
        public List<TransferOutRecordDto> OutRecords { get; set; } = new();
    }

    /// <summary>
    /// 调拨入库响应DTO
    /// </summary>
    public class TransferInResponse
    {
        public int TransferId { get; set; }
        public string TransferNo { get; set; } = string.Empty;
        public string Message { get; set; } = string.Empty;
        public List<TransferInRecordDto> InRecords { get; set; } = new();
    }

    /// <summary>
    /// 调拨出库记录DTO
    /// </summary>
    public class TransferOutRecordDto
    {
        public int Id { get; set; }
        public int TransferId { get; set; }
        public int LineId { get; set; }
        public int ProductId { get; set; }
        public string ProductName { get; set; } = string.Empty;
        public string ProductNo { get; set; } = string.Empty;
        public int WarehouseId { get; set; }
        public int? LocationId { get; set; }
        public string WarehouseName { get; set; } = string.Empty;
        public string LocationName { get; set; } = string.Empty;
        public decimal Qty { get; set; }
        public decimal UnitPrice { get; set; }
        public decimal Amount { get; set; }
        public string OperatorName { get; set; } = string.Empty;
        public DateTime OperatedAt { get; set; }
        public string Remark { get; set; } = string.Empty;
    }

    /// <summary>
    /// 调拨入库记录DTO
    /// </summary>
    public class TransferInRecordDto
    {
        public int Id { get; set; }
        public int TransferId { get; set; }
        public int LineId { get; set; }
        public int ProductId { get; set; }
        public string ProductName { get; set; } = string.Empty;
        public string ProductNo { get; set; } = string.Empty;
        public int WarehouseId { get; set; }
        public int? LocationId { get; set; }
        public string WarehouseName { get; set; } = string.Empty;
        public string LocationName { get; set; } = string.Empty;
        public decimal Qty { get; set; }
        public decimal UnitPrice { get; set; }
        public decimal Amount { get; set; }
        public string OperatorName { get; set; } = string.Empty;
        public DateTime OperatedAt { get; set; }
        public string Remark { get; set; } = string.Empty;
    }

    /// <summary>
    /// 调拨操作日志 DTO
    /// </summary>
    public class TransferOpLogDto
    {
        public int Id { get; set; }
        public int TransferId { get; set; }
        public string OpType { get; set; } = string.Empty;
        public string OpContent { get; set; } = string.Empty;
        public string OpUser { get; set; } = string.Empty;
        public DateTime CreatedAt { get; set; }
    }

}

